﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class control_LeftMenu : System.Web.UI.UserControl
{
    int catalogId = 0;
    int projectId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["id_user"] == null || Session["permission"] == null || ((int)Session["permission"]) <= 1)
            Response.Redirect("../login.aspx");

        if (((int)Session["permission"]) == 2 || ((int)Session["permission"]) == 3)
        {
            title_manager.Visible = true;
            menu_manager.Visible = true;
        }
        else
        {
            title_manager.Visible = false;
            menu_manager.Visible = false;
        }
        // get current project to display user from project
        try
        {
            catalogId = Int32.Parse(Request.QueryString["catalog"]);
        }
        catch
        {
            catalogId = 0;
        }
        if (catalogId != 0)
        {
            //get projectId
            string sql = "SELECT projectId FROM [catalog] WHERE id=" + comm.to_sql(catalogId.ToString(), "number");
            DataTable dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count == 1)
            {
                projectId = Int32.Parse(dtTable.Rows[0]["projectId"].ToString());
            }
        }
        else
        {
            try
            {
                projectId = Int32.Parse(Request.QueryString["project"]);
            }
            catch
            {
                projectId = 0;
            }
        }
        if (projectId != 0)
        {
            string sql = " SELECT userName,userId FROM [user],UserProject " +
                         " WHERE projectId=" + comm.to_sql(projectId.ToString(), "number") +
                         " AND [user].id=UserProject.userId";
            DataTable dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count >= 1)
            {
                foreach (DataRow row in dtTable.Rows)
                {
                    HtmlGenericControl li = new HtmlGenericControl("li");                
                    HyperLink link = new HyperLink();
                    link.NavigateUrl = "../home/Work.aspx?project="+projectId.ToString()+"&user="+row["userId"].ToString();
                    link.Text = row["username"].ToString();
                    li.Controls.Add(link);
                    listworks.Controls.Add(li);
                }
                HtmlGenericControl liAll = new HtmlGenericControl("li");
                HyperLink linkAll = new HyperLink();
                linkAll.NavigateUrl = "../home/Work.aspx?project=" + projectId.ToString();
                linkAll.Text = "Tất cả";
                liAll.Controls.Add(linkAll);
                listworks.Controls.Add(liAll);
            }
        }
    }
}
